package com.itouch.bracelet.activity;

import org.json.JSONObject;

import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.itouch.bracelet.R;
import com.itouch.bracelet.bean.BaseResult;
import com.itouch.bracelet.bean.LoginUser;
import com.itouch.bracelet.cache.UserCache;
import com.itouch.bracelet.diseaseinfo.Constants;

/**
 * cocoaSJ
 */
public class ChangePasswordActivity extends BaseActivity implements OnClickListener{

	private static final String LOG_TAG = "ChangePasswordActivity";
	private EditText cPassword;
	private EditText cRePassword;
	private TextView changePw;


	
	
	
	@Override
	protected int setContentViewById() {
		return R.layout.activity_change_password;
	}

	@Override
	protected void initViews() {
		cPassword = (EditText)findViewById( R.id.c_password );
		cRePassword = (EditText)findViewById( R.id.c_re_password );
		changePw = (TextView)findViewById( R.id.change_pw );
		changePw.setOnClickListener(this);
		
	}

	@Override
	protected void initData() {

	}
	
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.change_pw:
		String password=	cPassword .getText().toString();
	
		if(password==null||password.length()<5){
			Toast.makeText(context, R.string.change_pass,Toast.LENGTH_LONG).show();
			return;
		}	
		String repassword=	cRePassword .getText().toString();
		
		if(repassword==null||repassword.length()<5){
			Toast.makeText(context, R.string.change_repass,Toast.LENGTH_LONG).show();
			return;
		}	
		
		if(!repassword.equals(password)){
			Toast.makeText(context, R.string.change_equale,Toast.LENGTH_LONG).show();
			return;
		}
			
		changePassword(password);
			break;
		default:
			break;
		}
		
	}
	
	
	/**
	 * 修改密码
	 * @param password
	 */
	private void changePassword(String password) {
		LoginUser user =UserCache.getOauth(context);
		if(user==null){
			return;
		}
		show();
		Uri.Builder builder = Uri.parse(Constants.UPDATE_PASSWORD)
				.buildUpon();
		// http://127.0.0.1:8083/mobi/customer/updatePwdInside?id=87&password=111111
		builder.appendQueryParameter("id", user.getUserId());
		builder.appendQueryParameter("password",  password );
		
		Log.d("UPDATE_PASSWORD",
				"UPDATE_PASSWORD" + builder.toString());
		getJSONByVolleyGET(builder.toString(), 
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject arg0) {
						disMiss();
						Log.d("UPDATE_PASSWORD",
								"UPDATE_PASSWORD" + arg0.toString());
						try {
							BaseResult a = new Gson().fromJson(
									arg0.toString(), BaseResult.class);
							if (a == null) {
								return;
							}
							if (a.isFlag()) {
								Toast.makeText(context,
										R.string.change_pwd_ok,
										Toast.LENGTH_LONG).show();
								ChangePasswordActivity.this.finish();
							} else {
								Toast.makeText(context,
										a.getMsg() + "",
										Toast.LENGTH_LONG).show();
							}
						} catch (Exception e) {
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						disMiss();
						Toast.makeText(context,
								getString(R.string.net_error) + "",
								Toast.LENGTH_LONG).show();
					}
				});

	}

	
	
	
}
